﻿using System;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace XUBugly
{
    namespace Editor
    {
        [Serializable]
        internal class XUBuglyConfig
        {
            public static XUBuglyConfig LoadOrCreate()
            {
                XUBuglyConfig instance;

                string jsonPath = Application.streamingAssetsPath + "/BuglyConfig.json";
                if (File.Exists(jsonPath))
                {
                    string jsonText = File.ReadAllText(jsonPath, new UTF8Encoding(false));
                    instance = JsonUtility.FromJson<XUBuglyConfig>(jsonText);
                }
                else
                {
                    instance = new XUBuglyConfig();
                    instance.Save();
                }

                return instance;
            }

            public void Save()
            {
                string jsonPath = Application.streamingAssetsPath + "/BuglyConfig.json";
                string jsonText = JsonUtility.ToJson(this, true);
                if (File.Exists(jsonPath))
                {
                    string lastText = File.ReadAllText(jsonPath, new UTF8Encoding(false));
                    if (!jsonText.Equals(lastText))
                    {
                        File.WriteAllText(jsonPath, jsonText, new UTF8Encoding(false));
                    }
                }
                else
                {
                    if (!Directory.Exists(Path.GetDirectoryName(jsonPath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(jsonPath));
                    }
                    File.WriteAllText(jsonPath, jsonText, new UTF8Encoding(false));
                }
            }

            //=============================================================================================================

            public string androidId = "a7972208d3"; //xubugly-android
            public string androidKey = "dd3798df-04fe-44a4-bd48-2973a49db091"; //xubugly-android
            public string iOSId = "6c1161fd65"; //xubugly-ios
            public string iOSKey = "03cdff92-b6cc-4abf-a10c-940480f79932"; //xubugly-ios
            public bool uploadSymbols = true;

            public void OnGUI()
            {
                GUIStyle titleStyle = new GUIStyle(GUI.skin.GetStyle("Label"));
                titleStyle.alignment = TextAnchor.UpperCenter;
                titleStyle.normal.textColor = Color.green;

                GUILayout.Space(3);
                EditorGUILayout.LabelField("--------------------------- 安卓 ---------------------------", titleStyle);
                GUILayout.Space(3);
                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("App ID:", GUILayout.Width(60));
                    string androidIdText = EditorGUILayout.TextField(androidId);
                    androidId = !string.IsNullOrEmpty(androidIdText) ? androidIdText : androidId;
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("App Key:", GUILayout.Width(60));
                    string androidKeyText = EditorGUILayout.TextField(androidKey);
                    androidKey = !string.IsNullOrEmpty(androidKeyText) ? androidKeyText : androidKey;
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(3);
                EditorGUILayout.LabelField("--------------------------- 苹果 ---------------------------", titleStyle);
                GUILayout.Space(3);
                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("App ID:", GUILayout.Width(60));
                    string iOSIdText = EditorGUILayout.TextField(iOSId);
                    iOSId = !string.IsNullOrEmpty(iOSIdText) ? iOSIdText : iOSId;
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField("App Key:", GUILayout.Width(60));
                    string iOSKeyText = EditorGUILayout.TextField(iOSKey);
                    iOSKey = !string.IsNullOrEmpty(iOSKeyText) ? iOSKeyText : iOSKey;
                }
                GUILayout.EndHorizontal();

                GUILayout.Space(3);
                EditorGUILayout.LabelField("--------------------------- 其他 ---------------------------", titleStyle);
                GUILayout.Space(3);
                {
                    uploadSymbols = EditorGUILayout.Toggle("Upload Symbols", uploadSymbols);
                }
            }
        }
    }
}